package com.chess.annotation.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("ViewConstructor")
public class AnnotationTextView extends AnnotationView implements OnTouchListener {

	private int textInitSize = 0;
	private TextView textView;
	private EditText editText;
	private String text;

	public AnnotationTextView(Context c, String content) {
		super(c);
		this.text = content;
		this.textInitSize = getResources().getDimensionPixelSize(R.dimen.annotation_default_text_size);
		initContentView();
	}

	protected void initContentView() {

		LayoutParams paramsTextView = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		paramsTextView.addRule(CENTER_IN_PARENT);
		paramsTextView.leftMargin = operationRadius / 2;
		paramsTextView.rightMargin = operationRadius / 2;
		paramsTextView.topMargin = operationRadius / 2;
		paramsTextView.bottomMargin = operationRadius / 2;

		{
			textView = new TextView(context);
			textView.setGravity(Gravity.CENTER);
			textView.setText(this.text);
			textView.setTextSize(textInitSize);
			textView.setTextColor(Color.BLACK);
			textView.setBackgroundColor(getResources().getColor(R.color.abs__holo_blue_light_transparent));
			addView(textView, paramsTextView);
		}

		{
			editText = new EditText(context);
			editText.setGravity(Gravity.CENTER);
			editText.setText(this.text);
			editText.setTextSize(textInitSize);
			editText.setTextColor(Color.BLACK);
			editText.setVisibility(View.GONE);
			addView(editText, paramsTextView);
		}

		this.bringChildToFront(closeView);
		if (enableReshape) {
			this.bringChildToFront(reshapeView);
		}
	}

	@Override
	protected void updateContentView(float scaleNew, LayoutParams paramsTextView) {
		textView.setTextSize(scaleNew * textInitSize);
		editText.setTextSize(scaleNew * textInitSize);
		this.updateViewLayout(textView, paramsTextView);
		this.updateViewLayout(editText, paramsTextView);
	}

	protected void setOperationInvisible() {
		super.setOperationInvisible();
		textView.setBackgroundColor(0);
	}

	public void setOperationVisible() {
		super.setOperationVisible();
		textView.setBackgroundColor(getResources().getColor(R.color.abs__holo_blue_light_transparent));
	}

}